from apps.system.lessee.dao import LesseeDao
from apps.interface.client.dao import ClientDao
from apps.interface.tabbarItem.dao import TabbarItemDao, TabbarItemBaseSerializer
from apps.interface.pageConfig.dao import PageConfig, PageConfigDetailSerializer, PageConfigDao, PageConfigBaseSerializer
from apps.interface.advert.dao import Advert, AdvertDao
from apps.interface.node.dao import NodeDao
from apps.merchant.activity.dao import ActivityDao, ActivityBaseSerializer
from apps.system.rank.dao import RankDao, RankBaseSerializer
from apps.common.utils.tools import random_num, random_str
from apps.common.utils.decrypt import encrypt_MD5
from uuslug import slugify

def onekeyLessee(params, request):
    _lesseeName = params.get('name')

    default_lesseeId = params.get('lesseeId')

    # 创建一个lessee
    if _lesseeName:
        data = {}
        data['name'] = _lesseeName
        data['serviceChargeRate'] = 0.05
    new_lessee = LesseeDao().create(data)


    print('复制活动')
    activitys = ActivityBaseSerializer(ActivityDao().findAllByLesseeId(default_lesseeId), many=True).data
    for activity in activitys:
        activity['lessee'] = new_lessee.id
        ActivityDao().create(activity)

    print('复制级别')
    ranks = RankBaseSerializer(RankDao().findAllByLesseeId(default_lesseeId), many=True).data
    for rank in ranks:
        rank['lessee'] = new_lessee.id
        RankDao().create(rank)

    print('创建客户端')
    # 创建客户端
    clients = ClientDao().findAllByLesseeId(default_lesseeId)
    for client in clients:
        clientData = {}
        clientData['lessee'] = new_lessee.id
        clientData['types'] = client.types
        clientData['clientname'] = str(new_lessee.id) + '_' + client.types
        clientData['appname'] = '未设置'
        clientData['appid'] = client.appid
        clientData['appsecret'] = client.appsecret
        clientData['homePage'] = client.homePage
        clientData['baseUrl'] = client.baseUrl
        clientData['notify_url'] = client.notify_url
        clientData['notify_url_return'] = client.notify_url_return


        ClientDao().create(clientData)

    # 创建主菜单
    tabbarItems = TabbarItemBaseSerializer(TabbarItemDao().findAllByLesseeId(default_lesseeId).filter(isShow=True), many=True).data
    for tabbarItem in tabbarItems:
        tabbarItem['lessee'] = new_lessee.id
        TabbarItemDao().create(tabbarItem)

    # 创建界面
    print('创建界面')
    _clients = ClientDao().findAll().filter(lessee=new_lessee)
    default_client = ClientDao().findAll().filter(clientname=str(default_lesseeId) + '_' + 'minipro').first()
    default_pagesConfig = PageConfigDetailSerializer(PageConfig.objects.filter(clients=default_client, is_delete=0),
                                                     many=True).data
    for pageConfig in default_pagesConfig:
        pageConfig['clients'] = _clients.values_list('id', flat=True)
        serializer = PageConfigBaseSerializer(data=pageConfig)
        pageConfigObj = ''
        if serializer.is_valid():
            serializer.save()
            pageConfigObj = serializer.data
        else:
            print('error', serializer.errors)

        # 创建节点列表
        for nodeInfo in pageConfig.get('nodeList'):
            # 创建node
            nodeInfo['pageConfig'] = pageConfigObj.get('id')
            NodeDao().create(nodeInfo)

    # 创建管理员
    username = slugify(_lesseeName).replace('-', '') + random_num(4)
    createUser({
        'lessee': new_lessee.id,
        'role': 'lessee',
        'nickname': _lesseeName + '管理员',
        'username': username,
        "password": encrypt_MD5(username),
    })
    rootUser = createUser({
        "lessee": new_lessee.id,
        "role": "custom",
        "username": str(new_lessee.id) + "_root",
        "password": encrypt_MD5(str(new_lessee.id) + "@root"),
        "phone": "root",
        "is_root": 1
    })
    createUser({
        "lessee": new_lessee.id,
        "role": "custom",
        "username": str(new_lessee.id) + "_test",
        "password": encrypt_MD5('123456'),
        "phone": "test",
        "parent": rootUser.id
    })